package cn.botter.auth.application.controller;

import cn.botter.auth.application.convert.AuthRolePermissionConvert;
import cn.botter.auth.application.dto.AuthRoleDTO;
import cn.botter.auth.application.dto.AuthRolePermissionDTO;
import cn.botter.auth.application.exception.RegistrationException;
import cn.botter.auth.domain.entity.AuthRolePermissionBO;
import cn.botter.auth.domain.service.AuthRolePermissionDomainService;
import cn.botter.auth.entity.Result;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @BelongsProject: bo-club
 * @BelongsPackage: cn.botter.application.controller
 * @Author: 爱写Bug的Botter
 * @CreateTime: 2025-03-17:08:21
 * @Description: 描述
 * @Version: 1.0
 */
@RestController
@RequestMapping("/rolePermission/")
@Slf4j
public class AuthRolePermissionController {

    @Resource
    private AuthRolePermissionDomainService authRolePermissionDomainService;

    /**
     * 添加用户角色信息
     */
    @RequestMapping("add")
    public Result<AuthRoleDTO> add(@RequestBody AuthRolePermissionDTO authRolePermissionDTO) {
        try {
            Preconditions.checkNotNull(authRolePermissionDTO.getRoleId() , "用户角色id不能为空");
            Preconditions.checkNotNull(authRolePermissionDTO.getPermissionIdList() , "用户权限列表不能为空");
            if (log.isInfoEnabled()){
                log.info("AuthRolePermissionController.add.dto:{}", JSON.toJSONString(authRolePermissionDTO));
            }
            AuthRolePermissionBO authRolePermissionBO = AuthRolePermissionConvert.INSTANCE.convertDtoToBo(authRolePermissionDTO);
            Boolean result = authRolePermissionDomainService.add(authRolePermissionBO);
            if (result!=null && result){
                return Result.OK("添加用户角色权限成功!", result);
            }
            else {
                throw new RegistrationException("用户已存在");
            }
        } catch (Exception e) {
            log.info("AuthRoleController.add.error:{}", e.getMessage());
            return Result.FAILED("添加用户角色权限失败!",e.getMessage());
        }
    }



}
